var defaultReloadIntervals = [
  {string:"3 sec.",         ms:   3000, menuItemId:undefined},
  {string:"5 sec.",         ms:   5000, menuItemId:undefined},
  {string:"7 sec.",         ms:   7000, menuItemId:undefined},
  {string:"10 sec.",        ms:  10000, menuItemId:undefined},
  {string:"15 sec.",        ms:  15000, menuItemId:undefined},
  {string:"20 sec.",        ms:  20000, menuItemId:undefined},
  {string:"30 sec.",        ms:  30000, menuItemId:undefined},
  {string:"1 min.",         ms:  60000, menuItemId:undefined},
  {string:"1 min. 30 sec.", ms:  90000, menuItemId:undefined},
  {string:"2 min.",         ms: 120000, menuItemId:undefined},
  {string:"3 min.",         ms: 180000, menuItemId:undefined},
  {string:"5 min.",         ms: 300000, menuItemId:undefined},
  {string:"10 min.",        ms: 600000, menuItemId:undefined},
  {string:"15 min.",        ms: 900000, menuItemId:undefined},
  {string:"20 min.",        ms:1200000, menuItemId:undefined},
  {string:"30 min.",        ms:1800000, menuItemId:undefined},
  {string:"45 min.",        ms:2700000, menuItemId:undefined},
  {string:"1 hr.",          ms:3600000, menuItemId:undefined}
];
function createReloadMenus() {
  for (i in defaultReloadIntervals) {
    defaultReloadIntervals[i].menuItemId  = chrome.contextMenus.create({
      "title": "Reload page every " + defaultReloadIntervals[i].string,
      "type": "radio",
      "checked": false,
      "parentId": parent,
      "onclick": reloadClicked
    });
  }
}

function reloadClicked(info, tab) {
  var defaultReloadInterval;
  for (i in defaultReloadIntervals) {
    if (defaultReloadIntervals[i].menuItemId == info.menuItemId) {
      // Found which defaultReloadInterval correlates to the menu item clicked
      defaultReloadInterval = defaultReloadIntervals[i];
    } else {
      // Uncheck menu items that were not clicked
      chrome.contextMenus.update(
        defaultReloadIntervals[i].menuItemId, {"checked":false}
      );
    }
  }
  // Uncheck disable button
  chrome.contextMenus.update(radDisable, {"checked":false});

  // Start job
  var process = {
    action:"reload", tabIds:[tab.id],
    comment:"Reload selected tab"
  }
  startJob({
    processes:[process], singleTab:true, delay:defaultReloadInterval.ms,
    comment:"Reload selected tab every " + defaultReloadInterval.string
  });
}

function disableClicked(info, tab) {
  stopJob(undefined, tab.id);
  // Uncheck all menu items
  for (i in defaultReloadIntervals) {
    chrome.contextMenus.update(
      defaultReloadIntervals[i].menuItemId, {"checked":false}
    );
  }
}

// Update context menus for tabs
function updateTabMenus(tabId) {
  var matchFound = false;
  // Determine which defaultReloadInterval started this tab's job, if any
  allJobs(function(job, jobNum){
    if (job.singleTab && job.processes[0].tabIds[0] == tabId &&
        job.processes[0].action == "reload"){
      for (i in defaultReloadIntervals) {
        if (defaultReloadIntervals[i].ms == job.delay) {
          // Tab was started by a menu with defaultReloadInterval[i].menuItemId,
          // or something exactly like it.
          matchFound = true;
          chrome.contextMenus.update(radDisable, {"checked":false});
          chrome.contextMenus.update(
            defaultReloadIntervals[i].menuItemId, {"checked":true}
          );
        } else {
          chrome.contextMenus.update(
            defaultReloadIntervals[i].menuItemId, {"checked":false}
          );
        }
      }
    }   
  });
  
  if (!matchFound) {
    // Clear all defaultReloadInterval checked menus
    for (i in defaultReloadIntervals) {
      chrome.contextMenus.update(
        defaultReloadIntervals[i].menuItemId, {"checked":false}
      );
    }
    
    // Set "checked" on the disable tab menu item unless there is an active job
    if (!isTabInActiveJob(tabId)) {
      chrome.contextMenus.update(radDisable, {"checked":true});
    } else chrome.contextMenus.update(radDisable, {"checked":true});
  } 
  
}  

// Update context menus on tab change
chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo) {
  updateTabMenus(tabId);
});
// Update context menus on window change
chrome.windows.onFocusChanged.addListener(function(windowId) {
  if (windowId != -1) {
    chrome.tabs.getSelected(windowId, function(selectedTab){
      updateTabMenus(selectedTab.id);
    });
  }
});

// End jobs on tab close (also fired on tabs when a windows is closed)
chrome.tabs.onRemoved.addListener(function(tabId) {
  stopJob(undefined, tabId);
});

var parent = chrome.contextMenus.create({"title": "Chromatic"});
var radDisable = chrome.contextMenus.create({
  "title": "Disable for current tab",
  "type": "radio",
  "checked": true,
  "parentId": parent,
  "onclick":disableClicked
});
var seperator1 = chrome.contextMenus.create({
  "type": "separator", "parentId": parent
});
createReloadMenus();